package ui.panel
{

	import net.data.HeroInfo;
	import net.message.CG_Loading_HeroMessage;

	import ui.component.Page;
	import ui.panel.warriors.WarriorInfo;

	/**
	 * 英雄列表
	 * @author luguobin
	 */
	public class WarriorPanel extends BasePanel
	{
		private var _heroItems:Array;
		private var _page:Page;
		private var _isDispatch:Boolean; //是否是派驻
		private var _data:Vector.<HeroInfo>;

		public function WarriorPanel()
		{
			super();
			pid = PanelType.WARRIOR_PANEL;
			updateTitle("英雄列表");
		}

		override protected function initUI():void
		{
			super.initUI();
			_heroItems = [];

			for(var i:int = 0; i < 4; i++)
			{
				var item:WarriorInfo = new WarriorInfo();
				item.x = 25 + 143 * i;
				item.y = 50;
				this.addChild(item);
				_heroItems.push(item);
			}

			_page = new Page(pageHandler);
			_page.x = 250;
			_page.y = 395;
			this.addChild(_page);
		}

		private function pageHandler(num:uint):void
		{
			for(var i:int = 0; i < 4; i++)
			{
				var curIndex:int = (num - 1) * 4 + i;
				var item:WarriorInfo = _heroItems[i];

				if(curIndex < _data.length)
				{
					item.isDispatch = _isDispatch;
					item.update(_data[curIndex]);
				}
				else
				{
					item.clear();
				}
			}
		}

		/**
		 * 查看英雄列表
		 * **/
		public function getListData():void
		{
			clear();
			CG_Loading_HeroMessage.listType = CG_Loading_HeroMessage.WORRIOR_UI;
			var loading_hero:CG_Loading_HeroMessage = new CG_Loading_HeroMessage();
			loading_hero.writeMessage();
		}

		public function update(data:Vector.<HeroInfo>):void
		{
			_data = data;
			var len:int = data.length;
			_page.max = int(len / 4) + (len % 4 > 0 ? 1 : 0);
			pageHandler(1);
		}

		override public function clear():void
		{
			for(var i:int = 0; i < 4; i++)
			{
				var item:WarriorInfo = _heroItems[i];
				item.clear();
			}
		}

		public function get isDispatch():Boolean
		{
			return _isDispatch;
		}

		public function set isDispatch(value:Boolean):void
		{
			_isDispatch = value;
		}
	}
}